package com.foodorder.repository;

import com.foodorder.entity.StoreFollow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 店铺关注数据访问接口
 */
@Repository
public interface StoreFollowRepository extends JpaRepository<StoreFollow, Long> {
    
    /**
     * 查找用户关注的店铺
     */
    List<StoreFollow> findByUserIdOrderByFollowedAtDesc(Long userId);
    
    /**
     * 查找店铺的关注者
     */
    List<StoreFollow> findByStoreId(Long storeId);
    
    /**
     * 检查用户是否关注了店铺
     */
    Optional<StoreFollow> findByUserIdAndStoreId(Long userId, Long storeId);
    
    /**
     * 统计店铺关注数
     */
    long countByStoreId(Long storeId);
    
    /**
     * 统计用户关注的店铺数
     */
    long countByUserId(Long userId);
    
    /**
     * 查找接收通知的关注者
     */
    @Query("SELECT sf FROM StoreFollow sf WHERE sf.storeId = :storeId AND sf.receiveNotifications = true")
    List<StoreFollow> findNotificationEnabledFollowers(@Param("storeId") Long storeId);
    
    /**
     * 删除关注关系
     */
    void deleteByUserIdAndStoreId(Long userId, Long storeId);
} 